package com.ailanyin.modules.system.service.impl;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.ailanyin.common.poi.model.ExcelSetting;
import com.ailanyin.modules.system.domain.SysExcelSetting;
import com.ailanyin.modules.system.mapper.SysExcelSettingMapper;
import com.ailanyin.modules.system.service.ISysExcelSettingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

/**
 * Excel导出设置Service业务层处理
 *
 * @author ailanyin
 * @date 2022-10-25
 */
@Service
public class SysExcelSettingServiceImpl implements ISysExcelSettingService {

    @Resource
    private SysExcelSettingMapper sysExcelSettingMapper;

    /**
     * 查询Excel导出设置
     *
     * @param id Excel导出设置主键
     * @return Excel导出设置
     */
    @Override
    public SysExcelSetting selectSysExcelSettingById(Long id) {
        return sysExcelSettingMapper.selectSysExcelSettingById(id);
    }

    /**
     * 查询Excel导出设置列表
     *
     * @param className 类名
     * @return Excel导出设置
     */
    @Override
    public List<ExcelSetting> selectListByClassName(String className) {
        return sysExcelSettingMapper.selectListByClassName(className);
    }

    @Override
    public void insertSysExcelSetting(Class clazz, String className) {
        Field[] fields = clazz.getDeclaredFields();
        int j = 1;
        for (Field field : fields) {
            if (field.getAnnotation(Excel.class) != null) {
                Excel excel = field.getAnnotation(Excel.class);
                SysExcelSetting sysExcelSetting = new SysExcelSetting();
                sysExcelSetting.setClassName(className);
                sysExcelSetting.setFieldId(field.getName());
                sysExcelSetting.setFieldName(excel.name());
                // 宽度
                sysExcelSetting.setWidth(String.valueOf(excel.width()));
                // 排序
                sysExcelSetting.setSort(j++);
                // 时间格式化
                sysExcelSetting.setDateFormat(excel.format());
                // replace
                String[] replace = excel.replace();
                if (replace.length > 0) {
                    StringBuilder builder = new StringBuilder();
                    for (String str : replace) {
                        builder.append(str).append(",");
                    }
                    String reStr = builder.toString();
                    reStr = reStr.substring(0, reStr.length() - 1);
                    sysExcelSetting.setReplace(reStr);
                }
                sysExcelSetting.setShow(SysExcelSetting.EXCEL_FIELD_SHOW_FLAG);
                sysExcelSettingMapper.insertSysExcelSetting(sysExcelSetting);
            }
        }
    }

    /**
     * 修改Excel导出设置
     *
     * @param sysExcelSetting Excel导出设置
     * @return 结果
     */
    @Override
    public int updateSysExcelSetting(SysExcelSetting sysExcelSetting) {
        return sysExcelSettingMapper.updateSysExcelSetting(sysExcelSetting);
    }

    /**
     * 批量删除Excel导出设置
     *
     * @param ids 需要删除的Excel导出设置主键
     * @return 结果
     */
    @Override
    public int deleteSysExcelSettingByIds(List<Long> ids) {
        return sysExcelSettingMapper.deleteSysExcelSettingByIds(ids);
    }

    /**
     * 删除Excel导出设置信息
     *
     * @param id Excel导出设置主键
     * @return 结果
     */
    @Override
    public int deleteSysExcelSettingById(Long id) {
        return sysExcelSettingMapper.deleteSysExcelSettingById(id);
    }
}
